function getdiscount() {
    var qs = (location.search.length > 0 ? location.search.substring(1) : ""),
    args = {},
    items = qs.length ? qs.split("&") : [],
    item = null,
    name = null,
    value = null,
    i = 0,
    len = items.length;
    for (i = 0; i < len; i++) {
        item = items[i].split("=");
        name = decodeURIComponent(item[0]);
        value = decodeURIComponent(item[1]);
        if (name.length) {
            args[name] = value
        }
    }
    return args
}
var loop = true;
$(function() {
    var path = location.href;
    path = path.substr(path.lastIndexOf("/") + 1);
    path = path.split(".")[0];
    $("#public").after("<script src='" + path + ".js' ><\/script>");
    var silderHtml = "";
    if (data.corouselImg.length == 0) {
        data.corouselImg.push("http://www.wassk.cn/Myftp/upload/jlsj/file/noPic.jpg");
        loop = false
    }
	if(data.contentHtml!= undefined){
	    	var first= data.contentHtml.indexOf("<script");
		    var firstContent=data.contentHtml.substring(first+9);
		   var second= firstContent.indexOf('script>');
		   var fristHtml=data.contentHtml.substring(0,first);
		   var secondHmtl=firstContent.substring(second+8);
		   data.contentHtml=fristHtml+secondHmtl;
		   $("#bookDetailFrame").contents().find("body").append(data.contentHtml);
	    }
    for (var i = 0; i < data.corouselImg.length; i++) {
        silderHtml += '<div class="swiper-slide" onclick="fullScreen(' + i + ')"><img src="' + data.corouselImg[i] + '" class="img' + i + ' imgBlock"  onerror="errImg(' + i + ')"/></div>'
    }
    $(".mainIndex>.swiper-container .swiper-wrapper").append(silderHtml);
    $(".mainIndex>.swiper-container").css("height", $(".mainIndex .swiper-container").width());
    var swiper = new Swiper(".mainIndex>.swiper-container", {
        pagination: ".mainIndex>.swiper-container .swiper-pagination ",
        paginationClickable: true,
        loop: loop,
    });
    var bookdetailArr = new Array();
    if (data.introduction == undefined) {
        data.introduction = ""
    }
    if (data.relatedBook == undefined) {
        data.relatedBook = []
    }
    var relateBookNull = 0,
    silderbottomHtml = "";
    for (var i = 0; i < data.relatedBook.length; i++) {
        if (data.relatedBook[i].bookName == "") {
            relateBookNull += 1
        }
        if (data.relatedBook[i].price == "") {
            relateBookNull += 1
        }
        if (data.relatedBook[i].url == "") {
            relateBookNull += 1;
            relateBookNull = 3
        }
        if (relateBookNull != 3) {
            if (data.relatedBook[i].src == "") {
                data.relatedBook[i].src = "http://www.wassk.cn/Myftp/upload/jlsj/file/noPic.jpg"
            }
            var num = new Number(data.relatedBook[i].price);
            num = num.toFixed(2);
            silderbottomHtml += '<div class="swiper-slide swiper-slide-new" >' + '<a href="' + data.relatedBook[i].url + '">' + '<div class="bookintroImg">' + '<img src="' + data.relatedBook[i].src + '" class=" bookIntroImg' + i + '" onerror="errorBookDetailImg(' + i + ')"/>' + "</div>" + '<div class="bookDetailtext detailbookName">' + data.relatedBook[i].bookName + "</div>" + '<div class="bookDetailtext detailprice">￥' + num + "</div>" + "</a>" + "</div>"
        }
        relateBookNull = 0
    }
    if (silderbottomHtml == "") {
        $(".bookIntroDetailist").css("display", "none")
    }
    $(".mainIndex #bookDetail .swiper-container .swiper-wrapper").append(silderbottomHtml);
    if(data.corouselImg.length!=1){
    	var swiper2 = new Swiper(".mainIndex #bookDetail .swiper-container", {
            nextButton: ".swiper-button-next",
            prevButton: ".swiper-button-prev",
            slidesPerView: 3,
            spaceBetween: 10
        });
    }
    
    $(".arrow-left").on("click",
    function(e) {
        e.preventDefault();
        e.stopPropagation();
        mySwiper.swipePrev()
    });
    $(".arrow-right").on("click",
    function(e) {
        e.preventDefault();
        e.stopPropagation();
        mySwiper.swipeNext()
    });
    data.discount = getdiscount().discount;
    $(".bookName").text(data.bookName);
    $(".bookeasyintro").text(data.bookeasyintro);
    $(".oldPrice del").text("￥" + data.oldPrice);
    $(".newPrice").text("￥" + (data.oldPrice * data.discount / 100).toFixed(2));
    $(".discount").text(data.discount / 10 + "折");
    $(".bookauthor").text(data.author);
    $(".bookpress").text(data.press);
});
var acWidth = new Array();
var acHeight = new Array();
var boWidth = new Array();
var boHeight = new Array();
window.onload = function() {
    if (!loop) {
        $(".img0").css("margin-top", ($(".mainIndex .swiper-container").width() - 284) / 2 + "px")
        $(".img0").removeClass("imgBlock");
    } else {
        for (var i = 0; i < data.corouselImg.length; i++) {
            acWidth[i] = $(".img" + i).width();
            acHeight[i] = $(".img" + i).height();
            if (acWidth[i] > acHeight[i]) {
                $(".img" + i).attr("width", $(".mainIndex .swiper-container").width() + "px");
                $(".img" + i).css("margin-top", ($(".mainIndex .swiper-container").width() - $(".img" + i).height()) / 2 + "px")
            } else {
                $(".img" + i).attr("height", $(".mainIndex .swiper-container").width() + "px")
            }
            $(".img" + i).removeClass("imgBlock");
        }
    }
    for (var i = 0; i < data.relatedBook.length; i++) {
        boWidth[i] = Math.abs($(".bookIntroImg" + i).width() - $(".bookintroImg").width());
        boHeight[i] = Math.abs($(".bookIntroImg" + i).height() - $(".bookintroImg").height());
        if (acWidth[i] > acHeight[i]) {
            $(".bookIntroImg" + i).attr("height", $(".bookintroImg").height() + "px");
            $(".bookIntroImg" + i).css("margin-left", ($(".bookintroImg").width() - $(".bookIntroImg" + i).width()) / 2 + "px")
        } else {
            $(".bookIntroImg" + i).attr("width", $(".bookintroImg").width() + "px");
            $(".bookIntroImg" + i).css("margin-top", ($(".bookintroImg").height() - $(".bookIntroImg" + i).height()) / 2 + "px")
        }
    }
	var ifm= document.getElementById("bookDetailFrame");   
	var subWeb = document.frames ? document.frames["bookDetailFrame"].document : ifm.contentDocument;   
	if(ifm != null && subWeb != null) {
		$("#bookDetailFrame").css("height", subWeb.body.scrollHeight);
		ifm.height = subWeb.body.scrollHeight;
		ifm.width = subWeb.body.scrollWidth;
	} 
};
window.onresize = function() {
    $(".mainIndex .swiper-container").css("height", $(".swiper-container").width() + "px");
    window.onload()
};
function fullScreen(index) {
    $(".mainIndex").css("display", "none");
    $(".fullScreenDiv").css("display", "block");
    $(".fullScreenDiv .swiper-wrapper").children().remove();
    var silderfullHtml = "";
    for (var i = 0; i < data.corouselImg.length; i++) {
        silderfullHtml += '<div class="swiper-slide" onclick="cancelFull()"><img src="' + data.corouselImg[i] + '" class="fullImg' + i + '"  onerror="errorfullImg(' + i + ')"/>' + '<div class="pagePos">' + '<span class="nowpage">' + (i + 1) + "&nbsp;</span>&frasl;" + '<span class="totalpage">&nbsp;' + data.corouselImg.length + "</span>" + "</div>" + "</div>"
    }
    $(".fullScreenDiv .swiper-wrapper").append(silderfullHtml);
    $(".fullScreenDiv .swiper-container").css("height", $(window).width() + "px");
    $(".fullScreenDiv").css("margin-top", ($(window).height() - $(".fullScreenDiv .swiper-container").height()) / 2 + "px");
    if(data.corouselImg.length!=1){
    	var swiper = new Swiper(".fullScreenDiv .swiper-container", {
            loop: loop,
            initialSlide: index,
        });
    }
    
    if (!loop) {
        $(".fullImg0").css("margin-top", ($(".fullScreenDiv .swiper-container").width() - 284) / 2 + "px")
    } else {
        for (var i = 0; i < data.corouselImg.length; i++) {
            if (acWidth[i] > acHeight[i]) {
                $(".fullImg" + i).attr("width", $(".fullScreenDiv .swiper-container").width() + "px");
                $(".fullImg" + i).css("margin-top", ($(".fullScreenDiv .swiper-container").width() - $(".fullImg" + i).height()) / 2 + "px")
            } else {
                $(".fullImg" + i).attr("height", $(".fullScreenDiv .swiper-container").width() + "px")
            }
        }
    }
}
function cancelFull() {
    $(".mainIndex").css("display", "block");
    $(".fullScreenDiv").css("display", "none")
}
function errImg(index) {
    $(".img" + index).css("margin-top", (($(window).width() - 11) / 2 + "px"))
}
function errorfullImg(index) {
    $(".fullImg" + index).css("margin-top", (($(window).width() - 11) / 2 + "px"))
}
function errorBookDetailImg(index) {
    $(".bookIntroImg" + index).attr("src", "http://www.wassk.cn/Myftp/upload/jlsj/file/noPic.jpg")
};